package com.singfan.core.dao.database.impl;

import com.singfan.common.db.DaoBaseGeneric;
import com.singfan.core.dao.database.ICustomerTokenDao;
import com.singfan.core.dao.database.model.CustomerToken;
import org.springframework.stereotype.Repository;

import java.util.Date;

/**
 *
 * Author: hansonghe
 * Date: 2015-08-19 12:12
 * Desc:
 */
@Repository
public class CustomerTokenDaoImpl extends DaoBaseGeneric<CustomerToken> implements ICustomerTokenDao {
    @Override
    protected String getTableName(Object hashKey) {
        return tableNameBase;
    }

    @Override
    protected String getDatabaseId(Object hashKey) {
        return "app_1";
    }

    @Override
    public void insertOrUpdateToken(long customerId, String token, int cityId) {
        CustomerToken customerToken = new CustomerToken();
        customerToken.setCustomerId(customerId);
        customerToken.setAccessToken(token);
        customerToken.setCityId(cityId);
        customerToken.setTokenCreateTime(new Date());

        replace(customerId, customerToken);
    }

    @Override
    public CustomerToken findCustomerToken(String token) {
        String sql = "SELECT * FROM " + getTableName(null) + " WHERE access_token=?";
        return selectOne(null, sql, token);
    }

    @Override
    public void delete(long customerId) {
        deleteByPK(null, customerId);
    }
}
